//
// Created by 215609 on 2024/8/7.
//

#include "VideoDecoder.h"

void VideoDecoder::OnDecoderReady(){
    m_VideoWidth= GetCodecContext()->width;
    m_VideoHeight=GetCodecContext()->height;
    if (m_MsgCallback&&m_MsgCallback){
        m_MsgCallback(m_MsgContext,MSG_DECODER_READY,0);
    }
    if(m_VideoRender != nullptr) {

       int dstSize[2]={0};
        m_VideoRender->Init(m_VideoWidth, m_VideoHeight, dstSize);
        m_RenderWidth=dstSize[0];
        m_RenderHeight=dstSize[1];
        if (m_VideoRender->GetRenderType()==VIDEO_RENDER_ANWINDOW){
            int fps=25;
            long videoBitRate=m_VideoWidth*m_VideoHeight*fps*2;
            //录制视频
        }


        m_RGBAFrame=av_frame_alloc();
           //算出需要多少个字节
        int bufferSize= av_image_get_buffer_size(DST_PIXEL_FORMAT,
                                                 m_RenderWidth,m_RenderHeight,1);
        //分配内存
        m_FrameBuffer=(uint8_t *) av_malloc(bufferSize * sizeof(uint8_t));

        av_image_fill_arrays(m_RGBAFrame->data,m_RGBAFrame->linesize,
                             m_FrameBuffer, DST_PIXEL_FORMAT, m_RenderWidth, m_RenderHeight, 1);

        m_SwsContext= sws_getContext(m_VideoWidth,m_VideoHeight,GetCodecContext()->pix_fmt,
                                     m_RenderWidth,m_RenderHeight,DST_PIXEL_FORMAT,
                                     SWS_FAST_BILINEAR,NULL,NULL,NULL);

    }

//    if (m_V)
}




void VideoDecoder::OnFrameAvailable(AVFrame *frame){
    LOGCATE("OnFrameAvailable");
    if (m_VideoRender!= nullptr&&frame!= nullptr){
        NativeImage image;
        if (m_VideoRender->GetRenderType()==VIDEO_RENDER_ANWINDOW){
            if (m_SwsContext== nullptr){
                LOGCATE("OnFrameAvailable nulllll");
                return;
            }
            sws_scale(m_SwsContext,
                      frame->data,
                      frame->linesize,0,
                      m_VideoHeight,
                      m_RGBAFrame->data,
                      m_RGBAFrame->linesize);
            image.format=IMAGE_FORMAT_RGBA;
            image.width=m_RenderWidth;
            image.height=m_RenderHeight;
            image.ppPlane[0]=m_RGBAFrame->data[0];
            image.pLineSize[0]=image.width*4;
            m_VideoRender->RenderVideoFrame(&image);
        }

    }

}

void VideoDecoder::OnDecoderDone(){
    LOGCATE("VideoDecoder::OnDecoderDone");

    if(m_MsgContext && m_MsgCallback)
        m_MsgCallback(m_MsgContext, MSG_DECODER_DONE, 0);

    if(m_VideoRender)
        m_VideoRender->UnInit();

    if(m_RGBAFrame != nullptr) {
        av_frame_free(&m_RGBAFrame);
        m_RGBAFrame = nullptr;
    }

    if(m_FrameBuffer != nullptr) {
        free(m_FrameBuffer);
        m_FrameBuffer = nullptr;
    }

    if(m_SwsContext != nullptr) {
        sws_freeContext(m_SwsContext);
        m_SwsContext = nullptr;
    }

//    if(m_pVideoRecorder != nullptr) {
//        m_pVideoRecorder->StopRecord();
//        delete m_pVideoRecorder;
//        m_pVideoRecorder = nullptr;
//    }
}




